package com.oilsection.demo.service.impl;

import com.oilsection.demo.dao.*;
import com.oilsection.demo.po.*;
import com.oilsection.demo.entity.*;
import com.oilsection.demo.service.OverviewInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @Author yangyuge
 * @description
 * @Date 2024/5/3 16:12
 */
@Service
public class OverviewInfoServiceImpl implements OverviewInfoService {

    private static final Logger logger = LoggerFactory.getLogger(OverviewInfoServiceImpl.class);
    @Autowired
    private GetXldListSourceDao getXldListSourceDao;

    @Autowired
    private GetQkListSourceDao getQkListSourceDao;

    @Autowired
    private GetJcdListSourceDao getJcdListSourceDao;

    @Autowired
    private GetJzListSourceDao getJzListSourceDao;

    @Autowired
    private GetJcEventListSourceDao getJcEventListSourceDao;

    @Autowired
    private GetCkListSourceDao getCkListRepository;

    @Autowired
    private GetHkListSourceDao getHkListSourceDao;

    @Autowired
    private GetKqStateListSourceDao getKqStateListSourceDao;

    @Autowired
    private GetJzStateListSourceDao getJzStateListSourceDao;

    @Autowired
    private GetJkxlListSourceDao getJkxlListSourceDao;

    @Override
    //总览的库区信息
    public overviewKqInfo getQverviewKqInfo() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy");
        overviewKqInfo res = new overviewKqInfo();
        List<KqInfoTable> kqInfoTables = getQkListSourceDao.findAll();
        List<Map<String, String>> kqList = new ArrayList<>();
        List<Map<String, String>> kqAreaList = new ArrayList<>();
        List<Map<String, String>> kqJzNumList = new ArrayList<>();
        List<Map<String, List<String>>> runYearList = new ArrayList<>();
        Map<String, List<String>> yearMap = new HashMap<>();
        List<String> yearList = new ArrayList<>();
        List<String> kqNameList = new ArrayList<>();
        for (KqInfoTable ele : kqInfoTables) {
            Map<String, String> mapKq = new HashMap<>();
            Map<String, String> mapKqArea = new HashMap<>();
            Map<String, String> mapJzNum = new HashMap<>();
            mapKq.put("kq_id", ele.getId());
            mapKq.put("kq_name", ele.getName());
            mapKq.put("kq_area", String.valueOf(ele.getArea()));
            mapKq.put("jz_Num", String.valueOf(ele.getJzNum()));
            mapKq.put("kq_state", ele.getKqState());
            String d = sdf.format(ele.getStartTime());
            mapKq.put("startTime", d);
            String d1 = sdf.format(ele.getUpdateTime());
            mapKq.put("updateTime", d1);
            kqList.add(mapKq);

            kqNameList.add(ele.getName());
            int gap = Integer.valueOf(sdf1.format(ele.getUpdateTime())) - Integer.valueOf(sdf1.format(ele.getStartTime()));
            yearList.add(String.valueOf(gap));

            mapKqArea.put("name", ele.getName());
            mapKqArea.put("value", String.valueOf(ele.getArea()));
            kqAreaList.add(mapKqArea);

            mapJzNum.put("name", ele.getName());
            mapJzNum.put("value", String.valueOf(ele.getJzNum()));
            kqJzNumList.add(mapJzNum);
        }
        yearMap.put("kqNameList", kqNameList);
        yearMap.put("yearList", yearList);
        runYearList.add(yearMap);
        res.setKqList(kqList);
        res.setKqAreaList(kqAreaList);
        res.setKqJzNumList(kqJzNumList);
        res.setRunYearList(runYearList);

        return res;
    }


    //总览的检测点信息
    @Override
    public overviewJcdInfo getOverviewJcdInfo() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        overviewJcdInfo res = new overviewJcdInfo();
        List<Map<String, String>> jcdPie = new ArrayList<>();
        List<Map<String, String>> jcdList = new ArrayList<>();
        List<JcdInfoTable> jcdInfoTables = getJcdListSourceDao.findAll();
        List<KqInfoTable> kqInfoTables = getQkListSourceDao.findAll();
        for (JcdInfoTable ele : jcdInfoTables) {
            Map<String, String> map = new HashMap<>();
            map.put("kqId", ele.getKqId());
            KqInfoTable kqInfoTable = getQkListSourceDao.findById(ele.getKqId()).get();
            map.put("kqName", kqInfoTable.getName());
            map.put("jcdValue", String.valueOf(ele.getUpdateValue()));
            String d = sdf.format(ele.getUpdateTime());
            map.put("updateTime", d);
            map.put("jcdId", ele.getId());
            map.put("jcdName", ele.getJcdName());

            jcdList.add(map);
        }
        res.setJcdList(jcdList);
        for (KqInfoTable ele : kqInfoTables) {
            Map<String, String> map = new HashMap<>();
            map.put("name", ele.getName());
            map.put("value", String.valueOf(ele.getJcdNum()));
            jcdPie.add(map);
        }
        res.setJcdPie(jcdPie);
        return res;
    }

    //总览的历史状态
    public overviewHisInfo getOverviewHisInfo() {
        LocalDate date = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy");
        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("MM");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String curDate = date.format(formatter);
        String curYear = date.format(formatter1);
        String curMonth = date.format(formatter2);
        int monInt = 0;
        List<String> monthList = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            String mm = "";
            String yy = "";
            if (curMonth.indexOf("0") > -1) {
                monInt = Integer.parseInt(curMonth.substring(1,2)) - i;
            } else {
                monInt = Integer.parseInt(curMonth) - i;
            }
            if (monInt > 0) {
                yy = curYear;
                if (monInt < 10) {
                    mm = "0" + monInt;
                } else {
                    mm = String.valueOf(monInt);
                }
            } else {
                yy = String.valueOf(Integer.parseInt(curYear) - (Math.abs(monInt) / 12 + 1));
                monInt = (Math.abs(monInt) / 12 + 1) * 12 + monInt;
                if (monInt < 10) {
                    mm = "0" + monInt;
                } else {
                    mm = String.valueOf(monInt);
                }
            }
            monthList.add(yy + "-" + mm);
        }

        overviewHisInfo res = new overviewHisInfo();
        List<KqInfoTable> kqInfoTables = getQkListSourceDao.findAll();
        List<Map<String, List<String>>> jzStatelist = new ArrayList<>();
        for (KqInfoTable ele : kqInfoTables) {
            String kqId = ele.getId();
            Map<String, List<String>> jzMap = new HashMap<>();
            List<String> stateList = new ArrayList<>();
            for (int j = 0; j < 50; j++) {
                stateList.add("未检测");
            }
            List<KqStateInfoTable> kqStateInfoTables = getKqStateListSourceDao.findByKqId(kqId);
            for (KqStateInfoTable item : kqStateInfoTables) {
                String dd = sdf.format(item.getUpdateTime());
                int index = monthList.indexOf(dd);
                if (index > -1) {
                    stateList.set(index, item.getKqState());
                }
            }
            List<String> nameList = new ArrayList<>();
            nameList.add(ele.getName());
            jzMap.put("name",nameList);
            jzMap.put("jzStateList", stateList);
            jzStatelist.add(jzMap);
        }

        res.setMonthList(monthList);
        res.setKqStateList(jzStatelist);
        return res;
    }


    //总览的事故信息总览
    @Override
    public dmJceventInfo getOverviewEventInfo(String kqName, String dealSection, String eventType, String findTime, String jzId, String jzName, int page, int pageSize) {
        DecimalFormat df = new DecimalFormat("#.##");
        dmJceventInfo result = new dmJceventInfo();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        List<Map<String, String>> res = new ArrayList<>();
        List<Map<String, String>> listTemp = new ArrayList<>();
        List<KqInfoTable> kqInfoTables = getQkListSourceDao.findAll();
        for (KqInfoTable ele : kqInfoTables) {
            String id = ele.getId();
            List<JzInfoTable> jzInfoTables = getJzListSourceDao.findByKqId(id);
            for (JzInfoTable item : jzInfoTables) {
                List<XldInfoTable> xldInfoTables = new ArrayList<>();
                try{
                    xldInfoTables = getXldListSourceDao.findByJzId(item.getId());
                }catch(Exception e){}
                List<CkInfoTable> ckInfoTables = new ArrayList<>();
                try{
                    ckInfoTables = getCkListRepository.findByCkyIdOrSrId(item.getId(), item.getId());
                }catch(Exception e){}
                List<HkInfoTable> hkInfoTables = new ArrayList<>();
                try{
                    hkInfoTables = getHkListSourceDao.findByJzId(item.getId());
                }catch(Exception e){}
                List<JkxlInfoTable> jkxlInfoTables = new ArrayList<>();
                try{
                    jkxlInfoTables = getJkxlListSourceDao.findByXljzId(item.getId());
                }catch(Exception e){}

                for (XldInfoTable list : xldInfoTables) {
                    Map<String, String> map = new HashMap<>();
                    map.put("event_type", "泄漏");
                    map.put("kqName", id + ele.getName());
                    map.put("jzId", list.getJzId());
                    map.put("jzName", item.getJzName());
                    map.put("dealSection", list.getDealSection());
                    String d = sdf.format(list.getFindTime());
                    map.put("findTime", d);
                    String d1 = sdf.format(item.getUpdateTime());
                    map.put("updateTime", d1);
                    String xPoint = df.format(Double.parseDouble(list.getXldcXpoint())) + "°";
                    String yPoint = df.format(Double.parseDouble(list.getXldcYpoint())) + "°";
                    map.put("eventPos", "(" + xPoint + "," + yPoint + ")");
                    map.put("jcValue",list.getJcValue().toString());
                    res.add(map);
                }
                for (CkInfoTable list : ckInfoTables) {
                    Map<String, String> map = new HashMap<>();
                    map.put("event_type", "串库");
                    map.put("kqName", id + ele.getName());
                    map.put("jzId", list.getCkyId());
                    map.put("jzName", item.getJzName());
                    map.put("dealSection", list.getDealSection());
                    String d = sdf.format(list.getFindTime());
                    map.put("findTime", d);
                    String d1 = sdf.format(item.getUpdateTime());
                    map.put("updateTime", d1);
                    String reJz = "";
                    if (item.getId().equals(list.getCkyId())) {
                        reJz = list.getSrId();
                    } else if (item.getId().equals(list.getSrId())) {
                        reJz = list.getCkyId();
                    }
                    //TODO yyg修改
                    if(reJz.equals(""))
                    {

                    }else{
                        JzInfoTable jzInfoTable = getJzListSourceDao.findById(reJz).get();
                        String xPoint = df.format(Double.parseDouble(jzInfoTable.getJkXpoint())) + "°";
                        String yPoint = df.format(Double.parseDouble(jzInfoTable.getJkYpoint())) + "°";
                        map.put("eventPos", "(" + xPoint + "," + yPoint + ")");
                    }
                    map.put("jcValue",list.getJcValue());
                    res.add(map);
                }
                for (HkInfoTable list : hkInfoTables) {
                    Map<String, String> map = new HashMap<>();
                    map.put("event_type", "环空泄露");
                    map.put("kqName", id + ele.getName());
                    map.put("jzId", list.getJzId());
                    map.put("jzName", item.getJzName());
                    map.put("dealSection", list.getDealSection());
                    String d = sdf.format(list.getFindTime());
                    map.put("findTime", d);
                    String d1 = sdf.format(item.getUpdateTime());
                    map.put("updateTime", d1);
                    String xPoint = df.format(Double.parseDouble(list.getJzXpoint())) + "°";
                    String yPoint = df.format(Double.parseDouble(list.getJzYpoint())) + "°";
                    map.put("eventPos", "(" + xPoint + "," + yPoint + ")");
                    map.put("jcValue",list.getJcValue().toString());
                    res.add(map);
                }
                for (JkxlInfoTable list : jkxlInfoTables) {
                    Map<String, String> map = new HashMap<>();
                    map.put("event_type", "井口泄露");
                    map.put("kqName", id + ele.getName());
                    map.put("jzId", list.getXljzId());
                    map.put("dealSection", list.getDealSection());
                    String d = sdf.format(list.getFindTime());
                    map.put("findTime", d);
                    String d1 = sdf.format(item.getUpdateTime());
                    map.put("updateTime", d1);
                    map.put("eventPos", "");
                    map.put("jzName", item.getJzName());
                    map.put("jcValue",list.getJcValue().toString());
                    res.add(map);
                }
            }
        }

        for (int i = 0; i < res.size(); i++) {
            Map<String, String> map = res.get(i);
            if ((!kqName.equals("") ? kqName.equals(map.get("kqName")) : true) && (!dealSection.equals("") ? dealSection.equals(map.get("dealSection")) : true) &&
                    (!eventType.equals("")?eventType.equals(map.get("event_type")):true) && (!findTime.equals("") ? findTime.equals(map.get("findTime")) : true) &&
                    (!jzId.equals("") ? jzId.equals(map.get("jzId")) : true) && (!jzName.equals("") ? jzName.equals(map.get("jzName")) : true)
            ) {
                listTemp.add(map);
            }
        }
        int len = listTemp.size();
        List<Map<String, String>> list = new ArrayList<>();
        for (int j = page * pageSize; j < Math.min(len, (page + 1) * pageSize); j++) {
            Map<String, String> temp = listTemp.get(j);
            list.add(temp);
        }
        result.setJcEventList(list);
        result.setLen(len);
        return result;
    }

    public static String fillZero(int i) {
        String month = "";
        if (i < 10) {
            month = "0" + i;
        } else {
            month = String.valueOf(i);
        }
        return month;
    }

    //特定库区的库区信息
    @Override
    public List<Map<String, String>> getSpecKqKqInfo(String id) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        List<Map<String, String>> res = new ArrayList<>();
        List<JzInfoTable> jzInfoTables = getJzListSourceDao.findByKqId(id);
        for (JzInfoTable ele : jzInfoTables) {
            Map<String, String> map = new HashMap<>();
            map.put("jz_id", ele.getId());
            map.put("jcd_num", String.valueOf(ele.getJcdNum()));
            map.put("jz_name", ele.getJzName());
            map.put("jk_point", "(" + ele.getJkXpoint() + "," + ele.getJkYpoint() + ")");
            String d = sdf.format(ele.getUpdateTime());
            map.put("update_time", d);
            map.put("kq_state", ele.getJzState());
            res.add(map);
        }
        return res;
    }

    //特定库区的检测点信息
    @Override
    public dmJcdInfo getSpecKqJcdInfo(String jcEventId, String kqName, String jzId, String jzName, String jcdId, String jcdName, String jcState, int page, int pageSize, String id) {
        dmJcdInfo result = new dmJcdInfo();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        List<Map<String, String>> res = new ArrayList<>();
        List<JcEventInfoTable> jcEventInfoTables = getJcEventListSourceDao.findByKqId(id);
        List<JcEventInfoTable> jcInfoTemp = new ArrayList<>();
        for (int i = 0; i < jcEventInfoTables.size(); i++) {
            JcEventInfoTable ele = jcEventInfoTables.get(i);
            KqInfoTable kqInfoTemp = new KqInfoTable();
            try{
                kqInfoTemp = getQkListSourceDao.findById(ele.getKqId()).get();
            }catch (Exception e){}
            JzInfoTable jzInfoTemp = new JzInfoTable();
            try{
                jzInfoTemp = getJzListSourceDao.findById(ele.getJzId()).get();
            }catch (Exception e){ }
            JcdInfoTable jcdInfoTemp = new JcdInfoTable();
            try{
                jcdInfoTemp = getJcdListSourceDao.findById(ele.getJcdId()).get();
            }catch (Exception e){}

            float gap1 = Math.abs(jcdInfoTemp.getUpdateValue() - jcdInfoTemp.getReferValue());
            String jcStateTemp = "";
            if (gap1 < 0.5) {
                jcStateTemp = "正常";
            }else{
                jcStateTemp = "异常";
            }

            if ((!jcEventId.equals("") ? jcEventId.equals(ele.getId()) : true) && (!kqName.equals("") ? kqName.equals(kqInfoTemp.getId()+kqInfoTemp.getName()) : true) &&
                    (!jzId.equals("") ? jzId.equals(ele.getJzId()) : true) && (!jzName.equals("") ? jzName.equals(jzInfoTemp.getJzName()) : true) &&
                    (!jcdId.equals("") ? jcdId.equals(ele.getJcdId()) : true) && (!jcdName.equals("") ? jcdName.equals(jcdInfoTemp.getJcdName()) : true) &&
                    (!jcState.equals("") ? jcState.equals(jcStateTemp) : true)) {
                jcInfoTemp.add(ele);
            }
        }
        int len = jcInfoTemp.size();
        for (int j = page * pageSize; j < Math.min(len, (page + 1) * pageSize); j++) {
            JcEventInfoTable item = jcInfoTemp.get(j);
            KqInfoTable kqInfoTemp = new KqInfoTable();
            try{
                kqInfoTemp = getQkListSourceDao.findById(item.getKqId()).get();
            }catch(Exception e){}
            JzInfoTable jzInfoTemp = new JzInfoTable();
            try{
                jzInfoTemp = getJzListSourceDao.findById(item.getJzId()).get();
            }catch(Exception e){}
            JcdInfoTable jcdInfoTemp = new JcdInfoTable();
            try{
                jcdInfoTemp = getJcdListSourceDao.findById(item.getJcdId()).get();
            }catch(Exception e){}

            Map<String, String> map = new HashMap<>();
            map.put("jcEventId", item.getId());
            map.put("kqName", kqInfoTemp.getId() + kqInfoTemp.getName());
            map.put("jzId", item.getJzId());
            map.put("jzName", jzInfoTemp.getJzName());

            map.put("jcdId", item.getJcdId());
            map.put("jcdName", jcdInfoTemp.getJcdName()==null?"":jcdInfoTemp.getJcdName());
            map.put("jcValue", String.valueOf(item.getJcValue()));
            map.put("jcPoint", "(" + jcdInfoTemp.getJcdXpoint() + "," + jcdInfoTemp.getJcdYpoint() + ")");
            String d = sdf.format(item.getUpdateTime());
            map.put("updateTime", d);
            map.put("jcState", "正常");
            try {
                float gap = Math.abs(jcdInfoTemp.getUpdateValue() - jcdInfoTemp.getReferValue());
                if (gap < 0.5) {
                    map.put("jcState", "正常");
                } else {
                    map.put("jcState", "异常");
                }
            } catch (Exception e) {
            }
            res.add(map);
        }

        result.setJcdList(res);
        result.setLen(len);
        return result;
    }

    //特定库区的历史事件
    @Override
    public overviewHisInfo getSpecKqHisInfo(String id) {
        LocalDate date = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy");
        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("MM");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String curDate = date.format(formatter);
        String curYear = date.format(formatter1);
        String curMonth = date.format(formatter2);
        int monInt = 0;
        List<String> monthList = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            String mm = "";
            String yy = "";
            if (curMonth.indexOf("0") > -1) {
                monInt = Integer.parseInt(curMonth.substring(1, 2)) - i;
            } else {
                monInt = Integer.parseInt(curMonth) - i;
            }
            if (monInt > 0) {
                yy = curYear;
                if (monInt < 10) {
                    mm = "0" + monInt;
                } else {
                    mm = String.valueOf(monInt);
                }
            } else {
                yy = String.valueOf(Integer.parseInt(curYear) - (Math.abs(monInt) / 12 + 1));
                monInt = (Math.abs(monInt) / 12 + 1) * 12 + monInt;
                if (monInt < 10) {
                    mm = "0" + monInt;
                } else {
                    mm = String.valueOf(monInt);
                }
            }
            monthList.add(yy + "-" + mm);
        }
        overviewHisInfo res = new overviewHisInfo();
        List<JzInfoTable> jzInfoTables = getJzListSourceDao.findByKqId(id);
        List<Map<String, List<String>>> jzStatelist = new ArrayList<>();
        for (JzInfoTable ele : jzInfoTables) {
            String jzId = ele.getId();
            Map<String, List<String>> jzMap = new HashMap<>();
            List<String> stateList = new ArrayList<>();
            for (int j = 0; j < 50; j++) {
                stateList.add("未检测");
            }
            List<JzStateInfoTable> jzStateInfoTables = getJzStateListSourceDao.findByJzId(jzId);
            for (JzStateInfoTable item : jzStateInfoTables) {
                String dd = sdf.format(item.getUpdateTime());
                int index = monthList.indexOf(dd);
                if (index > -1) {
                    stateList.set(index, item.getJzState());
                }
            }
            List<String> nameList = new ArrayList<>();
            nameList.add(ele.getJzName());
            jzMap.put("name",nameList);
            jzMap.put("jzStateList", stateList);
            jzStatelist.add(jzMap);
        }
        res.setMonthList(monthList);
        res.setKqStateList(jzStatelist);
        return res;
    }

    //特定库区的事故信息总览
    @Override
    public dmJceventInfo getSpecKqEventInfo(String eventType, String findTime, String kqName, String dealSection, String jzId, String jzName, int page, int pageSize, String id) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        dmJceventInfo result = new dmJceventInfo();
        List<Map<String, String>> res = new ArrayList<>();
        KqInfoTable kqInfoTable=null;
        try {
            kqInfoTable = getQkListSourceDao.findById(id).get();
        }catch (Exception e)
        {
            System.out.println(e);
        }

        List<JzInfoTable> jzInfoTables = getJzListSourceDao.findByKqId(id);
        List<Map<String, String>> listTemp = new ArrayList<>();

        for (JzInfoTable item : jzInfoTables) {
            List<XldInfoTable> xldInfoTables = new ArrayList<>();
            try{
                xldInfoTables = getXldListSourceDao.findByJzId(item.getId());
            }catch(Exception e){}
            List<CkInfoTable> ckInfoTables = new ArrayList<>();
            try{
                ckInfoTables = getCkListRepository.findByCkyIdOrSrId(item.getId(), item.getId());
            }catch(Exception e){}
            List<HkInfoTable> hkInfoTables = new ArrayList<>();
            try{
                hkInfoTables = getHkListSourceDao.findByJzId(item.getId());
            }catch(Exception e){}
            List<JkxlInfoTable> jkxlInfoTables = new ArrayList<>();
            try{
                jkxlInfoTables = getJkxlListSourceDao.findByXljzId(item.getId());
            }catch(Exception e){}
            for (XldInfoTable list : xldInfoTables) {
                Map<String, String> map = new HashMap<>();
                map.put("event_type", "泄露");
                map.put("kqName", id + kqInfoTable.getName());
                map.put("jzId", list.getJzId());
                map.put("dealSection", list.getDealSection());
                String d = sdf.format(list.getFindTime());
                map.put("findTime", d);
                String d1 = sdf.format(item.getUpdateTime());
                map.put("updateTime", d1);
                map.put("eventPos", "");
                map.put("jzName", item.getJzName());
                map.put("jcValue",list.getJcValue().toString());
                res.add(map);
            }
            for (CkInfoTable list : ckInfoTables) {
                Map<String, String> map = new HashMap<>();
                map.put("event_type", "串库");
                map.put("kqName", id + kqInfoTable.getName());
                map.put("jzId", list.getCkyId());
                map.put("dealSection", list.getDealSection());
                String d = sdf.format(list.getFindTime());
                map.put("findTime", d);
                String d1 = sdf.format(item.getUpdateTime());
                map.put("updateTime", d1);
                map.put("eventPos", "");
                map.put("jzName", item.getJzName());
                map.put("jcValue",list.getJcValue());
                res.add(map);
            }
            for (HkInfoTable list : hkInfoTables) {
                Map<String, String> map = new HashMap<>();
                map.put("event_type", "环空泄露");
                map.put("kqName", id + kqInfoTable.getName());
                map.put("jzId", list.getJzId());
                map.put("dealSection", list.getDealSection());
                String d = sdf.format(list.getFindTime());
                map.put("findTime", d);
                String d1 = sdf.format(item.getUpdateTime());
                map.put("updateTime", d1);
                map.put("eventPos", "");
                map.put("jzName", item.getJzName());
                map.put("jcValue",list.getJcValue().toString());
                res.add(map);
            }
            for (JkxlInfoTable list : jkxlInfoTables) {
                Map<String, String> map = new HashMap<>();
                map.put("event_type", "井口泄露");
                map.put("kqName", id + kqInfoTable.getName());
                map.put("jzId", list.getXljzId());
                map.put("dealSection", list.getDealSection());
                String d = sdf.format(list.getFindTime());
                map.put("findTime", d);
                String d1 = sdf.format(item.getUpdateTime());
                map.put("updateTime", d1);
                map.put("eventPos", "");
                map.put("jzName", item.getJzName());
                map.put("jcValue",list.getJcValue().toString());
                res.add(map);
            }
        }
        for (int i = 0; i < res.size(); i++) {
            Map<String, String> map = res.get(i);
            if ((!eventType.equals("") ? eventType.equals(map.get("event_type")) : true) && (!findTime.equals("") ? findTime.equals(map.get("findTime")) : true) &&
                    (!kqName.equals("") ? kqName.equals(map.get("kqName")) : true) && (!jzId.equals("") ? jzId.equals(map.get("jzId")) : true) &&
                    (!jzName.equals("") ? jzName.equals(map.get("jzName")) : true) && (!dealSection.equals("") ? dealSection.equals(map.get("dealSection")) : true)) {
                listTemp.add(map);
            }
        }
        int len = listTemp.size();
        List<Map<String, String>> list = new ArrayList<>();
        for (int j = page * pageSize; j < Math.min(len, (page + 1) * pageSize); j++) {
            Map<String, String> temp = listTemp.get(j);
            list.add(temp);
        }

        result.setJcEventList(list);
        result.setLen(len);
        return result;
    }

    //井组的检测点信息
    @Override
    public dmJcdInfo getSpecJzJcdInfo(String jcEventId, String jcdId, String jcdName, String jzId, String jzName, String runState, String kqName, int page, int pageSize) {
        dmJcdInfo result = new dmJcdInfo();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        List<Map<String, String>> res = new ArrayList<>();
        List<JcEventInfoTable> jcEventInfoTables = getJcEventListSourceDao.findByJzId(jzId);
        List<JcEventInfoTable> listTemp = new ArrayList<>();
        for (int j = 0; j < jcEventInfoTables.size(); j++) {
            JcEventInfoTable item = jcEventInfoTables.get(j);
            JcdInfoTable jcdInfoTemp = new JcdInfoTable();
            try{
                jcdInfoTemp = getJcdListSourceDao.findById(item.getJcdId()).get();
            }catch (Exception e){}
            JzInfoTable jzInfoTemp = new JzInfoTable();
            try{
                jzInfoTemp = getJzListSourceDao.findById(item.getJzId()).get();
            }catch(Exception e){}
            KqInfoTable kqInfoTemp = new KqInfoTable();
            try{
                kqInfoTemp = getQkListSourceDao.findById(item.getKqId()).get();
            }catch (Exception e){}
            if ((!jcEventId.equals("") ? jcEventId.equals(item.getId()) : true) && (!jcdId.equals("") ? jcdId.equals(item.getJcdId()) : true) &&
                    (!jcdName.equals("") ? jcdName.equals(jcdInfoTemp.getJcdName()) : true) && (!jzName.equals("") ? jzName.equals(jzInfoTemp.getJzName()) : true) &&
                    (!runState.equals("") ? runState.equals(item.getJcValue()) : true) && (!kqName.equals("") ? kqName.equals(kqInfoTemp.getName()) : true)) {
                listTemp.add(item);
            }
        }
        int len = listTemp.size();
        for (int i = page * pageSize; i < Math.min(len, (page + 1) * pageSize); i++) {
            JcEventInfoTable ele = listTemp.get(i);
            Map<String, String> map = new HashMap<>();
            map.put("jcEventId", ele.getId());
            map.put("jzId", ele.getJzId());
            JzInfoTable jzInfoTable = getJzListSourceDao.findById(ele.getJzId()).get();
            map.put("jzName",jzInfoTable.getJzName());
            KqInfoTable kqInfoTable = getQkListSourceDao.findById(ele.getKqId()).get();
            map.put("kqName", kqInfoTable.getId() + kqInfoTable.getName());
            map.put("jcdId", ele.getJcdId());
            JcdInfoTable jcdInfoTable = getJcdListSourceDao.findById(ele.getJcdId()).get();
            map.put("jcdName", jcdInfoTable.getJcdName());
            if (Math.abs(ele.getJcValue() - jcdInfoTable.getReferValue()) > 0.5) {
                map.put("jcState", "异常");
            } else {
                map.put("jcState", "正常");
            }

            map.put("jcPoint", "(" + jcdInfoTable.getJcdXpoint() + "," + jcdInfoTable.getJcdYpoint());
            String d = sdf.format(ele.getUpdateTime());
            map.put("updateTime", d);
            res.add(map);
        }
        result.setLen(len);
        result.setJcdList(res);
        return result;
    }

    //井组的事故信息
    @Override
    public dmJceventInfo getSpecJzJcEventInfo(String eventType, String findTime, String kqName, String dealSection, String jzId, String jzName, int page, int pageSize) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        List<Map<String, String>> res = new ArrayList<>();
        dmJceventInfo result = new dmJceventInfo();
        List<XldInfoTable> xldInfoTables = new ArrayList<>();
        try{ xldInfoTables = getXldListSourceDao.findByJzId(jzId);}catch (Exception e){}
        List<CkInfoTable> ckInfoTables = new ArrayList<>();
        try{
            ckInfoTables = getCkListRepository.findByCkyIdOrSrId(jzId, jzId);
        }catch (Exception e){}
        List<HkInfoTable> hkInfoTables = new ArrayList<>();
        try{
            hkInfoTables = getHkListSourceDao.findByJzId(jzId);
        }catch (Exception e){}
        List<JkxlInfoTable> jkxlInfoTables = new ArrayList<>();
        try{
            jkxlInfoTables = getJkxlListSourceDao.findByXljzId(jzId);
        }catch (Exception e){}
        List<Map<String, String>> listTemp = new ArrayList<>();
        List<Map<String,String>> resTemp = new ArrayList<>();
        for (XldInfoTable list : xldInfoTables) {
            Map<String, String> map = new HashMap<>();
            JzInfoTable jzInfoTemp = getJzListSourceDao.findById(list.getJzId()).get();
            KqInfoTable kqInfoTemp = getQkListSourceDao.findById(jzInfoTemp.getKqId()).get();
            map.put("event_type", "泄露");
            map.put("kqName", kqInfoTemp.getId() + kqInfoTemp.getName());
            map.put("jzId", list.getJzId());
            map.put("dealSection", list.getDealSection());
            String d = sdf.format(list.getFindTime());
            map.put("findTime", d);
            String d1 = sdf.format(jzInfoTemp.getUpdateTime());
            map.put("updateTime", d1);
            map.put("eventPos", "");
            map.put("jcValue",list.getJcValue().toString());
            map.put("jzName", jzInfoTemp.getJzName());
            res.add(map);
        }
        for (CkInfoTable list : ckInfoTables) {
            Map<String, String> map = new HashMap<>();
            JzInfoTable jzInfoTemp = getJzListSourceDao.findById(jzId).get();
            KqInfoTable kqInfoTemp = getQkListSourceDao.findById(jzInfoTemp.getKqId()).get();
            map.put("event_type", "串库");
            map.put("kqName", kqInfoTemp.getId() + kqInfoTemp.getName());
            map.put("jzId", list.getCkyId());
            map.put("dealSection", list.getDealSection());
            String d = sdf.format(list.getFindTime());
            map.put("findTime", d);
            String d1 = sdf.format(jzInfoTemp.getUpdateTime());
            map.put("updateTime", d1);
            map.put("eventPos", "");
            map.put("jcValue",list.getJcValue());
            map.put("jzName", jzInfoTemp.getJzName());
            res.add(map);
        }
        for (HkInfoTable list : hkInfoTables) {
            Map<String, String> map = new HashMap<>();
            JzInfoTable jzInfoTemp = getJzListSourceDao.findById(list.getJzId()).get();
            KqInfoTable kqInfoTemp = getQkListSourceDao.findById(jzInfoTemp.getKqId()).get();
            map.put("event_type", "环空泄露");
            map.put("kqName", kqInfoTemp.getId() + kqInfoTemp.getName());
            map.put("jzId", list.getJzId());
            map.put("dealSection", list.getDealSection());
            String d = sdf.format(list.getFindTime());
            map.put("findTime", d);
            String d1 = sdf.format(jzInfoTemp.getUpdateTime());
            map.put("updateTime", d1);
            map.put("eventPos", "");
            map.put("jcValue",list.getJcValue().toString());
            map.put("jzName", jzInfoTemp.getJzName());
            res.add(map);
        }

        for (JkxlInfoTable list : jkxlInfoTables) {
            Map<String, String> map = new HashMap<>();
            JzInfoTable jzInfoTemp = getJzListSourceDao.findById(list.getXljzId()).get();
            KqInfoTable kqInfoTemp = getQkListSourceDao.findById(jzInfoTemp.getKqId()).get();
            map.put("event_type", "井口泄露");
            map.put("kqName", kqInfoTemp.getId() + kqInfoTemp.getName());
            map.put("jzId", list.getXljzId());
            map.put("dealSection", list.getDealSection());
            String d = sdf.format(list.getFindTime());
            map.put("findTime", d);
            String d1 = sdf.format(jzInfoTemp.getUpdateTime());
            map.put("updateTime", d1);
            map.put("eventPos", "");
            map.put("jcValue",list.getJcValue().toString());
            map.put("jzName", jzInfoTemp.getJzName());
            res.add(map);
        }
        for (int i = 0; i < res.size(); i++) {
            Map<String,String> map = res.get(i);

            if((!eventType.equals("")?eventType.equals(map.get("event_type")):true) && (!findTime.equals("")?findTime.equals(map.get("findTime")):true) &&
                    (!kqName.equals("")?kqName.equals(map.get("kqName")):true) && (!dealSection.equals("")?dealSection.equals(map.get("dealSection")):true) &&
                    (!jzId.equals("")?jzId.equals(map.get("jzId")):true) && (!jzName.equals("")?jzName.equals(map.get("jzName")):true)){
                resTemp.add(map);
            }
        }
        int len = resTemp.size();
        for (int j = page * pageSize; j < Math.min(len, (page + 1) * pageSize); j++) {
            Map<String, String> temp = resTemp.get(j);
            listTemp.add(temp);
        }
        result.setJcEventList(listTemp);
        result.setLen(len);
        return result;
    }

    //井组信息的历史信息
    public overviewHisInfo getSpecJzHisInfo(String jzId) {
        LocalDate date = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy");
        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("MM");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String curDate = date.format(formatter);
        String curYear = date.format(formatter1);
        String curMonth = date.format(formatter2);
        int monInt = 0;
        List<String> monthList = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            String mm = "";
            String yy = "";
            if (curMonth.indexOf("0") > -1) {
                monInt = Integer.parseInt(curMonth.substring(1)) - i;
            } else {
                monInt = Integer.parseInt(curMonth) - i;
            }
            if (monInt > 0) {
                yy = curYear;
                if (monInt < 10) {
                    mm = "0" + monInt;
                } else {
                    mm = String.valueOf(monInt);
                }
            } else {
                yy = String.valueOf(Integer.parseInt(curYear) - (Math.abs(monInt) / 12 + 1));
                monInt = (Math.abs(monInt) / 12 + 1) * 12 + monInt;
                if (monInt < 10) {
                    mm = "0" + monInt;
                } else {
                    mm = String.valueOf(monInt);
                }
            }
            monthList.add(yy + "-" + mm);
        }

        overviewHisInfo res = new overviewHisInfo();
        List<JcdInfoTable> jcdInfoTables = getJcdListSourceDao.findByJzId(jzId);
        List<Map<String, List<String>>> jzStatelist = new ArrayList<>();
        for (JcdInfoTable ele : jcdInfoTables) {
            String jcdId = ele.getId();
            Map<String, List<String>> jzMap = new HashMap<>();
            List<String> stateList = new ArrayList<>();
            for (int j = 0; j < 50; j++) {
                stateList.add("未检测");
            }
            List<JcEventInfoTable> jcEventInfoTables = getJcEventListSourceDao.findByJcdId(jcdId);
            for (JcEventInfoTable item : jcEventInfoTables) {
                String dd = sdf.format(item.getUpdateTime());
                int index = monthList.indexOf(dd);
                if (index > -1) {
                    String jcdState = "未检测";
                    if (item.getJcValue() - ele.getReferValue() >= 0.5) {
                        jcdState = "异常";
                    } else if ((item.getJcValue() - ele.getReferValue() >= 0) && (item.getJcValue() - ele.getReferValue() < 0.5)) {
                        jcdState = "正常";
                    }
                    stateList.set(index, jcdState);
                }
            }
            List<String> nameList = new ArrayList<>();
            nameList.add(ele.getJcdName());
            jzMap.put("name",nameList);
            jzMap.put("jzStateList", stateList);
            jzStatelist.add(jzMap);
        }

        res.setMonthList(monthList);
        res.setKqStateList(jzStatelist);
        return res;
    }
}
